package com.thread.lianxi;

public class TestVolatile {
	boolean status = false;

	/**
	 * 状态切换为true
	 */
	public void changeStatus() {
		status = true;
	}

	/**
	 * 若状态为true，则running。
	 */
	public void run() {
		if (status) {
			System.out.println("running....");
		} else {
			System.out.println("waiting....");
		}
	}

	public static void main(String[] args) {
		final TestVolatile vol = new TestVolatile();

		Thread t1 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("修改状态了。。。");
				vol.changeStatus();
			}

		});
		Thread t2 = new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					vol.run();
					if(vol.status){
						vol.run();
						break;
					}
				}
			}

		});

		t2.start();
		t1.start();
	}
}